package com.platform.controller;

import java.util.List;
import java.util.Map;

import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import com.platform.entity.TeamRecommendEntity;
import com.platform.service.TeamRecommendService;
import com.platform.utils.PageUtils;
import com.platform.utils.Query;
import com.platform.utils.R;

/**
 * 团队推荐人Controller
 *
 * @author liweihai
 * @email liweihai12345@sina.com
 * @date 2019-06-04 17:37:12
 */
@RestController
@RequestMapping("teamrecommend")
public class TeamRecommendController {
    @Autowired
    private TeamRecommendService teamRecommendService;

    /**
     * 查看列表
     */
    @RequestMapping("/list")
    @RequiresPermissions("teamrecommend:list")
    public R list(@RequestParam Map<String, Object> params) {
        //查询列表数据
        Query query = new Query(params);

        List<TeamRecommendEntity> teamRecommendList = teamRecommendService.queryList(query);
        int total = teamRecommendService.queryTotal(query);

        PageUtils pageUtil = new PageUtils(teamRecommendList, total, query.getLimit(), query.getPage());

        return R.ok().put("page", pageUtil);
    }

    /**
     * 查看信息
     */
    @RequestMapping("/info/{uid}")
    @RequiresPermissions("teamrecommend:info")
    public R info(@PathVariable("uid") Long uid) {
        TeamRecommendEntity teamRecommend = teamRecommendService.queryObject(uid);

        return R.ok().put("teamRecommend", teamRecommend);
    }

    /**
     * 保存
     */
    @RequestMapping("/save")
    @RequiresPermissions("teamrecommend:save")
    public R save(@RequestBody TeamRecommendEntity teamRecommend) {
        Integer count = teamRecommendService.queryUserExists(teamRecommend.getUid().intValue());
        if(count>0){
            return R.error("此用户已存在推荐表!");
        }
        teamRecommendService.save(teamRecommend);
        return R.ok();
    }

    /**
     * 修改
     */
    @RequestMapping("/update")
    @RequiresPermissions("teamrecommend:update")
    public R update(@RequestBody TeamRecommendEntity teamRecommend) {
        teamRecommendService.update(teamRecommend);

        return R.ok();
    }

    /**
     * 删除
     */
    @RequestMapping("/delete")
    @RequiresPermissions("teamrecommend:delete")
    public R delete(@RequestBody Long[] uids) {
        teamRecommendService.deleteBatch(uids);

        return R.ok();
    }

    /**
     * 查看所有列表
     */
    @RequestMapping("/queryAll")
    public R queryAll(@RequestParam Map<String, Object> params) {

        List<TeamRecommendEntity> list = teamRecommendService.queryList(params);

        return R.ok().put("list", list);
    }

    /**
     * 根据手机号查询信息
     * @param phone
     * @return
     */
    @RequestMapping("/queryObjectByPhone")
    public R queryObjectByPhone(@RequestParam("phone")String phone){
        Map<String,Object> map = teamRecommendService.queryUserDetails(phone);
        return R.ok().put("model",map);
    }
}
